<?php
/**
 * Patients Main Screen
 * This is patient summary screen,to handle monitorings and 
 * edit patient information.
 * 
 * @author Xavier VILAIN
 * @package Nawa\MedicalStudy\MedicalCenter\Patient
 * @license #license-nawa#
 * 
 */
use Nawa\MedicalStudy\MedicalCenter\Patient;
use Fox\Generator;
use Fox\Html\Html;
use Fox\Html\Block;
use Fox\Html\Screen;
// Initialize
$xview = Xview::instance();
$msid = $xview->val('msid');
$mcid = $xview->val('mcid');
$keys = array('msid' => $msid,'mcid' => $mcid);
$ms = $xview->component('medicalstudies','object',$keys);
$mcPartner = $xview->component('medicalstudies','medicalcenter',$keys);
$medicalCenter = $mcPartner->medicalCenter();

// Load Patient Data
$patients = $mcPartner->patients();

// Bread Crumb
$crumbs = Xview::instance()
	->component('medicalstudies','breadcrumb',
	array('deepness' => 4,
		  'customerid' => $ms->customer()->getId(),
		  'customer_name' => $ms->customer()->name1,
		  'msReference' => $ms->reference,
		  'msid' => $ms->getId(),
		  'msCenterCount' => $ms->countMedicalCentersPartners(),
		  'mcid' => $mcid,
		  'mcLabel' => $medicalCenter->label(),
		  'mcPatientCount' => count($patients),
		  'type' => Patient::BREADCRUMB_NODE
	)
);
$screen = new Screen('sPatiens');
$screen->breadcrumb()->crumbs($crumbs);

// Load Available Modules
$positions = array();
foreach($ms->followup() as $followup) {
	$positions[$followup->itemid] = $followup->position;
}

// Display all patient with there one monitoringe list
foreach($patients as $patient) {
	$label = $patient->reference;
	$pKeys = array_merge($keys,array('patientid' => $patient->patientid));
	$url = Generator::generateValue(Generator::url('medicalstudies','patient',null,$pKeys),array());
	$htmlBl = Msg(Nawa\Messages::BUNDLE,Nawa\Messages::PATIENT_TITLE,$label);
	$blPatient = new Html('bPatient_'.$patient->patientid,'<h3><a href="'.$url.'">'.$htmlBl.'</a></h3>');
	$screen->add($blPatient);
	// Monitoring List
	$monitoringGridModel = $xview->component('medicalstudies','monitorings',array(
		'msid'		=> $msid,
		'mcid'		=> $mcid,
		'patientid' => $patient->patientid
	));
	$monitoringGrid = clone $monitoringGridModel;
	$monitoringGrid->id('gMonitorings_'.$patient->patientid);
	$monitoringData = array();

	foreach($patient->monitorings() as $monitoring) {
		$data = $monitoring->getData();
		if(!empty($positions[$monitoring->itemid])) {
			$data['position'] = $positions[$monitoring->itemid];
		} else {
			$data['position'] = 0;
		}
		$visit = $patient->findVisits($msid.':'.$mcid.':'.$patient->patientid.':'.$monitoring->itemid);
		if($visit->exists()) {
			$data['visit_date'] = $visit->visit_date;
		}
		// Find Order
		$monitoringData[$monitoring->itemid] = $data;
	}
	// Order By Position
	uasort($monitoringData,function($a, $b){
			if($a['position'] > $b['position'])	{
				return 1;
			}
			return -1;}
	);
	$monitoringGrid->initialize($monitoringData);
	
	$screen->add($monitoringGrid);
	
}
echo $screen->display();